import common from '../common'
import {encodeQuery, delQueStr} from 'utils/helper/query'
import ENV from 'utils/env'
import storage from 'utils/helper/storage'
import ajax from '@/api'

const LOGINHOST = {// 不同环境m站登录地址 process.env.NODE_ENV
  // development: '//ssologindev.bestwehotel.com/#/',
  development: '//ssologintest.bestwehotel.com/#/',
  test: '//ssologintest.bestwehotel.com/#/',
  prev: '//ssologinpre.bestwehotel.com/#/',
  production: '//ssologin.bestwehotel.com/#/'
}


export default {
  ...(uni || {}),
  // 组好不要全部结构common，按需引入即可
  navigateTo: common.navigateTo,
  payOrderSDK: common.payOrderSDK,
  callPhone: common.callPhone,
  gotoHomePage: common.gotoHomePage,
  getOffset: common.getOffset,
  getAllOffset: common.getAllOffset,
  getSelectCity: common.getSelectCity,
  selectCity: common.selectCity,
  getSafeAreaHeight: common.getSafeAreaHeight,
  // getLocation: common.getLocation,
  getLocation() {
    return new Promise((resolve, reject) => {
      resolve()
    })
  },
  
  initTitle() {
    return new Promise((resolve, reject) => {
      resolve()
    })
  },

  login(params) { // m站登录
    
    // const token =  storage.get('AUTHORIZATION')
    // if(token){
    //   ajax.setConfig({
    //     header: {
    //       Authorization: token
    //     }
    //   })
    // }

    let data = params || {}
    let {notCallLogin} = data 
    if(notCallLogin) return true

    let callUrl = window.location.href
    callUrl = delQueStr(callUrl,'mToken')
    const loginParams = {
      succCallbackUrl: callUrl,
      cancelCallbackUrl: callUrl
    }

    return new Promise((resolve, reject) => {      
      window.location.href = `${LOGINHOST[ENV]}?${encodeQuery(loginParams)}`
      resolve()
    })
  },
  openMapVC(params) {
  },
  /**
   * 获取状态栏高度（仅适用于webview包含状态栏的情形）
   */
  getStatusBarHeight() {
    return 0
  },
  // 复制
  setClipboardData(data) {
    return new Promise((success, fail) => {
        const textarea = document.createElement('textarea');
        textarea.value = data;
        textarea.readOnly = 'readOnly';
        document.body.appendChild(textarea);
        textarea.select();
        textarea.setSelectionRange(0, data.length);
        document.execCommand('copy');
        textarea.remove();
        success(data);
    })
  },
  // 获取剪切板内容
  getClipboardData() {
    return new Promise((success, fail) => {
      navigator.clipboard.readText().then(success).catch(fail);
    });
  }
}
